//
//  UIImageView+HHExtension.m
//  Smart
//
//  Created by Hayder on 2022/10/21.
//

#import "UIImageView+HHExtension.h"
#import <YYImage/YYImage.h>
#import "HHGlobalDefine.h"
#import <YYWebImage/YYWebImage.h>

@implementation UIImageView (HHExtension)

- (void)setWebImage:(NSString *)urlString
{
    if(!urlString)
    {
        urlString = @"";
    }
    UIImage *placeHolder = [UIImage imageWithColor:kGray_240];
    NSString *iconUrl = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [self yy_setImageWithURL:[NSURL URLWithString:iconUrl] placeholder:placeHolder];

}
- (void)setWebImage:(NSString *)urlString placeHolder:(UIImage *)image{
    if(!urlString)
    {
        urlString = @"";
    }
    NSString *iconUrl = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [self yy_setImageWithURL:[NSURL URLWithString:iconUrl] placeholder:image];
}

//将ImageView上的控件合成image用于服务器上传
- (UIImage *)creatImage
{
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, self.opaque, [[UIScreen mainScreen] scale]);//图形以选项开始图像上下文
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();//结束图像上下文
    return img;
}

@end
